package me.xplabs.model.map 
{
	import flash.geom.Point;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import me.xplabs.astar.IMapTileModel;
	import me.xplabs.constant.GridType;
	import me.xplabs.Globals;
	import me.xplabs.model.combatunit.MBuildInfo;
	import me.xplabs.model.RolePositionManager;
	/**
	 * 地图数据
	 * ...
	 * @author xiaohan
	 */
	public class MMapData implements IMapTileModel
	{
		/** 地图的背景资源号*/
		public var mapRes:int;
		/** 地图的横向格子数量*/
		public var cellRow:int;
		/** 地图的纵向格子数量*/
		public var cellCol:int;
		/** 地图的网格障碍*/
		public var grids:Vector.<Vector.<int>>;
		/** 地图的建筑信息*/
		public var baseBuilds:Vector.<MBuildInfo>;
		/** 地图中格子上面的角色引用列表*/
		private var _paths:Dictionary;
		public function MMapData() 
		{
		}

		public function isBlock(p_startX:int, p_startY:int, p_endX:int, p_endY:int):int 
		{
			if (p_endX < 0 || p_endY < 0 || p_endX > grids.length - 1 || p_endY > grids[0].length - 1) return  GridType.UNABEL;
			return grids[p_endX][p_endY];
		}
		public function dispose():void
		{
			
		}
		/**
		 * 初始化网格
		 */
		public function initObstacle():void
		{
			grids = new Vector.<Vector.<int>>();
			for (var i:int = 0; i < cellRow; i++) 
			{
				grids[i] = new Vector.<int>();
				for (var j:int = 0; j < cellCol; j++) 
				{
					grids[i][j] = GridType.UNABEL;
				}
			}
			RolePositionManager.i.grids = grids;
		}
		/**
		 * 设置网格的障碍点
		 * @param	data
		 */
		public function setMapData(data:Vector.<Point>):void
		{
			dipose();
			var len:int = data.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (data[i].x > grids.length - 1 || data[i].y > grids[0].length - 1) continue;
				grids[data[i].x][data[i].y] = GridType.ABLE;
			}
		}
		/**
		 * 设置建筑信息列表
		 * @param	buildData
		 */
		public function setBuilData(buildData:Vector.<MBuildInfo>):void 
		{
			baseBuilds = buildData;
		}
		/**
		 * 解析地图.map数据
		 * @param	data
		 */
		public function formatMapData(data:ByteArray):void 
		{
			mapRes = data.readShort();
			cellRow = data.readShort();
			cellCol = data.readShort();

			var gridData:Vector.<Point> = new Vector.<Point>();
			var len:int = data.readShort();
			for (var i:int = 0; i < len; i++) 
			{
				var pt:Point = new Point(data.readShort(), data.readShort());
				gridData.push(pt);
			}
			initObstacle();
			setMapData(gridData);
			
			var buildData:Vector.<MBuildInfo> = new Vector.<MBuildInfo>();
			len = data.readShort();
			for (i = 0; i < len; i++)
			{
				var build:MBuildInfo = new MBuildInfo(int(data.readUTF()), data.readShort(), data.readShort());
				build.soleId = Globals.soleId;
				buildData.push(build);
			}
			setBuilData(buildData);
		}
		public function dipose():void
		{
			RolePositionManager.i.dipose();
		}
	}
}